from aiogram import types, exceptions
from aiogram.utils.keyboard import InlineKeyboardBuilder

from keyboards.callbacks_data import MenuCategory
from loader import db, bot, logger


def admin_menu():
    keys = InlineKeyboardBuilder()
    keys.button(text="Стоп слова", callback_data=MenuCategory(level=1, category="stop_words")),
    keys.button(text="Дефолтные слова", callback_data=MenuCategory(level=1, category="default_words")),
    keys.button(text="Обязательные каналы",
                callback_data=MenuCategory(level=1, category="require_channels")),
    keys.button(text="Реклама", callback_data=MenuCategory(level=1, category="ads")),
    keys.button(text="Статистика", callback_data=MenuCategory(level=1, category="statistic")),
    keys.adjust(1)
    return keys.as_markup()


def client_menu():
    keys = InlineKeyboardBuilder()
    keys.button(text="Стоп слова", callback_data=MenuCategory(level=1, category="stop_words"))
    keys.button(text="Обязательные каналы",
                callback_data=MenuCategory(level=1, category="require_channels")),
    # keys.button(text="Мои группы", callback_data="my_groups"),

    keys.adjust(1)
    return keys.as_markup()


def action(level, category, chat_id=None):
    keys = InlineKeyboardBuilder()
    keys.button(text="Добавить",
                callback_data=MenuCategory(level=level, category=category, action='add', chat_id=chat_id))
    keys.button(text="Удалить",
                callback_data=MenuCategory(level=level, category=category, action='delete', chat_id=chat_id))
    keys.button(text="Назад",
                callback_data=MenuCategory(level=level - 1, category=category, chat_id=chat_id, action='back'))
    keys.adjust(1)
    return keys.as_markup()


async def list_channels_buttons(level, client_id, chat_id=None, action='list'):
    keys = InlineKeyboardBuilder()
    channels = db.select_channels_by_chat(client_id, chat_id, action)
    for channel in channels:
        try:
            channel_entity = await bot.get_chat(int(channel))
            keys.button(text=channel_entity.full_name,
                        callback_data=MenuCategory(level=level, category="require_channels", action=action,
                                                   chat_id=chat_id, req_channel_id=channel_entity.id)),
        except exceptions.TelegramBadRequest:
            continue
    keys.button(text="Назад",
                callback_data=MenuCategory(level=level - 1, category="require_channels", chat_id=chat_id,
                                           action='back'))
    keys.adjust(1)
    return keys.as_markup()


async def list_groups_buttons(client_id):
    keys = InlineKeyboardBuilder()
    channels = db.select_channels_by_user(client_id)
    for channel in channels:
        channel_entity = await bot.get_chat(int(channel))
        keys.button(text=channel_entity.full_name,
                    callback_data=MenuCategory(level=2, category="require_channels", chat_id=channel_entity.id)),
    keys.button(text="Назад",
                callback_data=MenuCategory(level=1, category="require_channels", action='back'))
    keys.adjust(1)
    return keys.as_markup()


async def list_subs_keys(req_channels, user_id):
    keys = InlineKeyboardBuilder()
    chats_entity = [await bot.get_chat(c) for c in req_channels]
    for chat in chats_entity:
        keys.button(text=chat.title, url=chat.invite_link)
    keys.button(text='✅ Проверить подписку',
                callback_data=MenuCategory(level=10, category='require_channels', chat_id=user_id, action='check'))
    keys.adjust(1)
    return keys.as_markup()


def default_words():
    keys = InlineKeyboardBuilder()
    keys.button(text="Да", callback_data="stop_words:yes")
    keys.button(text="Нет", callback_data="stop_words:no")
    return keys.as_markup()


def back(level, category):
    keys = InlineKeyboardBuilder()
    keys.button(text="Назад", callback_data=MenuCategory(level=level, category=category, action='back'))
    keys.adjust(1)
    return keys.as_markup()


def ads_done(level, category):
    keys = InlineKeyboardBuilder()
    keys.button(text="Готово", callback_data=MenuCategory(level=level, category=category, action='done'))
    keys.button(text="Назад", callback_data=MenuCategory(level=level, category=category, action='back'))
    keys.adjust(1)
    return keys.as_markup()


def ads_list():
    keys = InlineKeyboardBuilder()
    ads_list = db.select_all_ads()
    for ad in ads_list:
        keys.button(text=ad.text[:20] if not ad.path_photo else '📷 ' + ad.text[:20],
                    callback_data=MenuCategory(level=2, category='ads', chat_id=ad.id))
    keys.button(text="Добавить", callback_data=MenuCategory(level=1, category='ads', action="add"))
    keys.button(text="Назад", callback_data=MenuCategory(level=1, category='ads', action='back'))
    keys.adjust(1)
    return keys.as_markup()


def ads_action(level, category, ads_id=None):
    keys = InlineKeyboardBuilder()
    keys.button(text="Запустить",
                callback_data=MenuCategory(level=level, category=category, action='start', chat_id=ads_id))
    keys.button(text="Удалить",
                callback_data=MenuCategory(level=level, category=category, action='delete', chat_id=ads_id))
    keys.button(text="Назад",
                callback_data=MenuCategory(level=level - 1, category=category, chat_id=ads_id, action='back'))
    keys.adjust(1)
    return keys.as_markup()
